<template>
  <div class="login">
    <div class="bg">
      <div class="nav">今日头条</div>
      <van-form ref="form">
        <van-field
          v-model="form.phone"
          required
          left-icon="phone-o"
          placeholder="请输入手机号"
          :rules="rules.phone"
        ></van-field>

        <van-field
          v-model="form.code"
          center
          clearable
          left-icon="bag-o"
          placeholder="请输入短信验证码"
          :rules="rules.code"
        >
          <template #button>
            <van-button
              size="small"
              type="primary"
              class="code"
              @click="sendcode"
              >发送验证码</van-button
            >
          </template>
        </van-field>
      </van-form>
      <div>
        <van-button
          block
          type="info"
          native-type="submit"
          class="button"
          @click="btnenter"
          >登录</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { v10Authorizations } from '@/api/login.js'
import { setLocal } from '@/utils/local.js'
export default {
  name: 'login',
  data () {
    return {
      form: {
        phone: '', // 手机号
        code: '' // 验证码
      },
      // 校验
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'onChange' },
          {
            validator: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '请输入正确的手机号',
            trigger: 'onChange'
          }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'onChange' }]
      }
    }
  },
  methods: {
    // 发送验证码
    sendcode () {
      // this.code = 246810
      this.$toast.success('246810')
    },
    // 登录
    async btnenter () {
      console.log(this.form.phone)
      // 判断如果手机号是错误的提示用户
      if (this.form.phone !== '13999999999' && this.form.code !== '246810') {
        // 提示用户
        this.$toast.fail('您输入正确的手机号或者验证码有误，请重新输入!')
      } else {
        const res = await v10Authorizations({
          mobile: this.form.phone,
          code: this.form.code
        })
        console.log(res)
        // 保存token
        setLocal('token', res.data.data.token)
        // 跳转到我们页面
        this.$router.push('/my')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  background-image: url('../../style/Snipaste_2021-01-03_15-16-02.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  height: 667px;
  padding-top: 200px;
  .bg {
    opacity: 0.9;
    padding: 0 38px;
  }
  .nav {
    height: 50px;
    font-size: 18px;
    color: #217eda;
    text-align: center;
    line-height: 50px;
    font-size: 30px;
    padding-bottom: 50px;
    font-weight: 1000;
  }
  .code {
    border-radius: 20px;
  }
  .button {
    margin-top: 20px;
    width: 300px;
    opacity: 0.9;
    // margin-left: -1px;
    border-radius: 40px;
    // background-color: #d0584d;
  }
}
</style>
